package com.caseProcess.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caseProcess.entity.*;
import com.caseProcess.service.PageService;
import com.caseProcess.service.VCaseEndService;
import com.caseProcess.utils.PagingUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ahuo
 * @version 1.0
 * @date 2020年11月15日 14点41分
 */
@RestController
@RequestMapping("/vce")
public class VCaseEndController {
    @Resource
    VCaseEndService vCaseEndService;
    @Resource
    private PageService pageService;

    /**
     * 查找当前部门的办结案件
     * @param request
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selectEndCaseByDepartment")
    public Result selectEndCaseByDepartment(HttpServletRequest request,@RequestParam(required = false,defaultValue = "0")int page,
                                            @RequestParam(required = false,defaultValue = "0")int limit) {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        VUserInfo vUserInfo= (VUserInfo) object;
        List<VCaseEnd> list= new ArrayList<VCaseEnd>();
        VCaseEnd vCaseEnd=new VCaseEnd();
        vCaseEnd.setResultStatusId(5);
        vCaseEnd.setProcDepartmentId(vUserInfo.getDepartmentId());
        list=vCaseEndService.selectEndCase(vCaseEnd);
        if(page!=0&&limit!=0){
            list= PagingUtil.paging(list,page,limit);
        }
        if (list.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(list);
            result.setCount(list.size());
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setCount(0);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     * 按条件查询办结案件
     * @param caseId 案件编号
     * @param procLevel 督办等级
     * @param caseDescription 警情描述
     * @param startTime 选择的开始时间
     * @param endTime 选择的结束时间
     * 查询的是在以上两个时间范围内的相关分派时间的案件
     */
    @RequestMapping("/selectByStamentByDepartment")
    public Result selectByStamentByDepartment(String caseId,String procLevel,String caseDescription,String startTime,String endTime,HttpServletRequest request,@RequestParam(required = false,defaultValue = "0")int page,
                                              @RequestParam(required = false,defaultValue = "0")int limit) {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        VUserInfo vUserInfo= (VUserInfo) object;
        List<VCaseEnd> list= new ArrayList<VCaseEnd>();
        VCaseEnd vCaseEnd=new VCaseEnd();
        vCaseEnd.setCaseId(caseId);
        vCaseEnd.setProcLevel(procLevel);
        vCaseEnd.setCaseDescription(caseDescription);
        vCaseEnd.setResultStatusId(5);
        vCaseEnd.setProcDepartmentId(vUserInfo.getDepartmentId());
        list=vCaseEndService.selectByStatment(vCaseEnd,startTime,endTime);
        result.setCount(list.size());
        if(page!=0&&limit!=0){
            list= PagingUtil.paging(list,page,limit);
        }
        if (list.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(list);
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }


    /**
     * 指挥中心办结，分页，查询
     * @param selectConditionJson
     * @param page
     * @param limit
     * @return 操作结果
     */
    @RequestMapping("/getEndCase")
    public Result getCases(String selectConditionJson, int page, int limit) {
        SelectCaseEndCondition selectCaseEndCondition = JSONObject.parseObject(selectConditionJson, SelectCaseEndCondition.class);

        String strWhere = "";
       // strWhere += "isStartProcess='" + selectCondition.getStartProcess() +"'";
        strWhere+="1=1";
        if (!selectCaseEndCondition.getCaseId().equals("") && !selectCaseEndCondition.getCaseId().equals(null)) {
            strWhere += " and caseid like concat('%','" + selectCaseEndCondition.getCaseId() + "','%')";
        }
        if (!selectCaseEndCondition.getProcLevel().equals("") && !selectCaseEndCondition.getProcLevel().equals(null)) {
            strWhere += " and proclevel = '" + selectCaseEndCondition.getProcLevel()+"'";
        }
        if (!selectCaseEndCondition.getProcDepartmentId().equals("") && !selectCaseEndCondition.getProcDepartmentId().equals(null)) {
            strWhere += " and procdepartmentid = '" + selectCaseEndCondition.getProcDepartmentId()+"'";
        }
        if (!selectCaseEndCondition.getCaseDescription().equals("") && !selectCaseEndCondition.getCaseDescription().equals(null)) {
            strWhere += " and casedescription like concat('%','" + selectCaseEndCondition.getCaseDescription() + "','%')";
        }
        if (!selectCaseEndCondition.getStartTime().equals("") && !selectCaseEndCondition.getStartTime().equals(null)) {

            strWhere += " and askForTime >='" + selectCaseEndCondition.getStartTime()+"'";
        }
        if (!selectCaseEndCondition.getEndTime().equals("") && !selectCaseEndCondition.getEndTime().equals(null)) {
            strWhere += " and askForTime <='" + selectCaseEndCondition.getEndTime()+"'";
        }
        //strWhere +="order by asktime desc";
        Page pg = new Page("V_CaseEnd", "caseid",1 , true, "*", limit, page, strWhere);

        List<Map<String, Object>> maps = pageService.selectByPage(pg);
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(maps);
        List<VCaseEnd> caseList = jsonArray.toJavaList(VCaseEnd.class);
        Result result = new Result();
        if (caseList.size() > 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(caseList);
            result.setCount(Long.parseLong(maps.get(0).get("PageCount") + ""));
        } else if (caseList.size() == 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("无数据");
            result.setData(caseList);
            result.setCount(0);
        }else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }


}
